package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends WebDriverHelperBase {

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
		
	}
	
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		List<WebElement> rows = driver.findElements(By.xpath("//tr[@name = 'entry']"));
		for (WebElement row : rows) {
			String firstname = row.findElement(By.xpath("./td[3]")).getText();
			String lastname = row.findElement(By.xpath("./td[2]")).getText();
			String email = row.findElement(By.xpath("./td[4]")).getText();
			String home = row.findElement(By.xpath("./td[5]")).getText();
			cachedContacts.add(new ContactData()
				.withFirstname(firstname)
				.withLastname(lastname)
				.withEmail(email)
				.withHome(home));
		}
		
		/*List<WebElement> fnamelist = driver.findElements(By.xpath("//tr[@name = 'entry']/td[3]"));
		List<WebElement> lnamelist = driver.findElements(By.xpath("//tr[@name = 'entry']/td[2]"));
		List<WebElement> homelist = driver.findElements(By.xpath("//tr[@name = 'entry']/td[4]"));
		List<WebElement> tellist = driver.findElements(By.xpath("//tr[@name = 'entry']/td[5]"));
		for (int i = 0; i < fnamelist.size(); i++) {
			String firstname = fnamelist.get(i).getText();
			String lastname = lnamelist.get(i).getText();
			String email = homelist.get(i).getText();
			String home = tellist.get(i).getText();
			cachedContacts.add(new ContactData()
					.withFirstname(firstname)
					.withLastname(lastname)
					.withEmail(email)
					.withHome(home));*/
		}
	
	

	public ContactHelper createContact(ContactData contact, boolean CREATION) {
		manager.navigateTo().mainPage();
		initNewContactCreation();
		fillContactForm(contact, CREATION);
		replacementOfEmptyFieldsInContact(contact);
		submitContactCreation();
		returnToHomePage();
		rebuildCache();
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact,
			boolean mODIFICATION2) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		fillContactForm(contact, MODIFICATION);
		replacementOfEmptyFieldsInContact(contact);
		submitContactModification();
		returnToHomePage();
		rebuildCache();
		return this;
	} 
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		selectContactByIndex(index);
		submitContactDeletion();
		returnToHomePage();
		rebuildCache();
		return this;
	}

	
//----------------------------------------------------------
	
	public ContactHelper initNewContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstname());
		type(By.name("lastname"), contact.getLastname());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getHome());
		type(By.name("mobile"), contact.getMobile());
		type(By.name("work"), contact.getWork());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		selectByText(By.name("bday"), contact.getBday());
		selectByText(By.name("bmonth"), driver.findElement(By.xpath("//select[2]/option[" + contact.getBmonth() + "]")).getText());
		type(By.name("byear"), contact.getByear());
		if (formType == CREATION) {
			//selectByText(By.name("new_group"), "group 1");
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0) {
				throw new Error("Group selector exists in contact modification form");
			}
		}
//		type(By.name("new_group"), contact.group);
//		new Select(driver.findElement(By.name("new_group"))).selectByVisibleText(contact.group);
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getPhone2());
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper returnToHomePage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper initContactModification(int index) {
		selectContactByIndex(index);		
		return this;
	}

	private void selectContactByIndex(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index+1) +"]"));
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("//input[@value='Update']"));
		cachedContacts = null;
		return this;
	}


	// method removing spaces from a phone number
	// because on main page this field is showed without spaces
	public ContactData removeSpacesFromAPhoneNumber(ContactData contact) {
		String homeWithoutSpaces = contact.getHome().replaceAll(" ", "");
		return contact.withHome(homeWithoutSpaces);
	}
	
	public ContactHelper replacementOfEmptyFieldsInContact(ContactData contact) {
		if (contact.getEmail().equals("")) 
			contact.withEmail(driver.findElement(By.name("email2")).getAttribute("value"));
		if (contact.getHome().equals("")) 
			contact.withHome(driver.findElement(By.name("mobile")).getAttribute("value"));
		if (contact.getHome().equals("")) 
			contact.withHome(driver.findElement(By.name("work")).getAttribute("value"));
		return this;
	}
	
	private void submitContactDeletion() {
		click(By.xpath("//input[@value='Delete']"));
		cachedContacts = null;
	}


}

